﻿using HAF.LogUtility;
using HAF.LogUtility.Except;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MediaTransfer.Impl
{
    [Serializable]
    public class BaseWorker : PushMessageable, IWorkItem
    {
        public BaseWorker() { }
        /// <summary>
        /// 工作项编号
        /// </summary>
        public long ID { get; set; }
        /// <summary>
        /// 标识
        /// </summary>
        public string Tag { get; set; }
        /// <summary>
        /// 归属任务编号
        /// </summary>
        /// <remarks>默认使用GUID值</remarks>
        public string TaskID { get; set; } = Guid.NewGuid().ToString("N");
        /// <summary>
        /// 工作项名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 工作项展示名称
        /// </summary>
        public string Caption { get; set; }
        /// <summary>
        /// 工作项优先级
        /// </summary>
        public int Priority { get; set; } = 4;
        /// <summary>
        /// 运行模式
        /// </summary>
        public int Model { get; set; } = 0;
        /// <summary>
        /// 工作项动作
        /// </summary>
        public int WorkAction { get; set; }
        /// <summary>
        /// 工作项归属分组
        /// </summary>
        public int WorkGroup { get; set; }
        /// <summary>
        /// 任务类型 此处和分组有什么区别？
        /// </summary>
        public int WorkType { get; set; } = 0;
        /// <summary>
        /// 工作项状态
        /// </summary>
        public int Statu { get; set; } = 1;
        /// <summary>
        /// 任务状态
        /// </summary>
        public int TaskState { get; set; }
        /// <summary>
        /// 工作项轮询时长
        /// </summary>
        public int Times { get; set; }
        /// <summary>
        /// 工作项失败已重试次数
        /// </summary>
        /// <remarks>原定义为可重试次数，现更改为已重试次数，每个工作项可重试次数由主配置决定，自己不再配置</remarks>
        public int RedoTimes { get; set; }
        /// <summary>
        /// 工作项关联编号 
        /// </summary>
        /// <remarks>主要用于关联入参表和出参表，也常用于日志输出文件名称前缀</remarks>
        public string WorkID { get; set; }
        /// <summary>
        /// 工作项内容（对象）
        /// </summary>
        public object WorkItems { get; set; }
        /// <summary>
        /// 工作项内容（文本）
        /// </summary>
        public string WorkJson { get; set; }

        /// <summary>
        /// 工作项执行指令
        /// </summary>
        public virtual void ExecItem()
        {
            
            WriteInfoMessage("工作项 TaskID {0} TaskName {1} WorkID {2} 启动", TaskID, string.IsNullOrEmpty(this.Caption) ? this.Name : this.Caption, WorkID);

        }

        /// <summary>
        /// 更新任务状态
        /// </summary>
        /// <param name="taskId">任务编号</param>
        /// <param name="iState">状态</param>
        public virtual void UpdateTaskState(string taskId, int iState)
        {
            if (string.IsNullOrEmpty(taskId))
            {
                throw new RunException(RunCode.ParamsCheckError);
            }
        }
        /// <summary>
        /// 自动保存 
        /// </summary>
        /// <remarks>在多个工作项同步工作时需配置为false</remarks>
        public bool AutoSave { get; set; }
        /// <summary>
        /// 工作项回调函数
        /// </summary>
        protected void ReturnItem()
        {
            EndAutoProcess("main", true);
            //写SysMonitor系统日志
            WriteInfoMessage("采集服务：{0}工作项完成", string.IsNullOrEmpty(this.Caption) ? this.Name : this.Caption);
            //LogHelper.WriteInfoLog(string.Format("采集服务：{0}工作项完成", string.IsNullOrEmpty(this.Caption) ? this.Name : this.Caption));
        }

        /// <summary>
        /// 重置工作项任务数据
        /// </summary>
        public void Reset()
        {

        }
        /// <summary>
        /// 工作项结果集
        /// </summary>
        public object ResultItems { get; set; }


        /// <summary>
        /// 添加推送消息
        /// </summary>
        /// <param name="msg"></param>
        public void AddOutMessage(string msg)
        {
            //if (OutMessage.MessageSet == null)
            //{
            //    OutMessage.MessageSet = new List<string>();
            //}
            //if (!string.IsNullOrEmpty(msg))
            //{
            //    OutMessage.MessageSet.Add(msg);
            //}
        }

        public List<string> ProcessBarIDs { get; set; }
        public Action<string, int> InitProcess { get; set; }
        public Action<string, int> ShowStep { get; set; }
        public Action<string, bool> EndProcess { get; set; }

        #region Release Private Method

        protected void InitAutoProcess(string processid, int iLength)
        {
            if (!string.IsNullOrEmpty(processid) && InitProcess != null)
            {
                InitProcess(processid, iLength);
            }
        }

        protected void ShowAutoStep(string processid, int iStep)
        {
            if (!string.IsNullOrEmpty(processid) && ShowStep != null)
            {
                ShowStep(processid, iStep);
            }
        }

        protected void EndAutoProcess(string processid, bool bRtn)
        {
            if (!string.IsNullOrEmpty(processid) && EndProcess != null)
            {
                EndProcess(processid, bRtn);
            }
        }

        #endregion Release Private Method

        /// <summary>
        /// 工作项创建人
        /// </summary>
        public string CreateUser { get; set; } = "WedoIptv";
        /// <summary>
        /// 工作项创建时间
        /// </summary>
        public DateTime CreateTime { get; set; } = DateTime.Now;
        /// <summary>
        /// 工作项开始时间
        /// </summary>
        public DateTime BeginTime { get; set; } = DateTime.Now;
        /// <summary>
        /// 工作项结束时间
        /// </summary>
        public DateTime EndTime { get; set; } = DateTime.Now;
        /// <summary>
        /// 是否支持远程执行
        /// </summary>
        public bool AllowRemoteExec { get; set; } = false;
        /// <summary>
        /// 远程执行地址
        /// </summary>
        public string HostHandle { get; set; }
        
    }
}
